package org.example.ttms.controller;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.layout.StackPane;
import javafx.util.Callback;
import org.example.ttms.customize.Detail;
import org.example.ttms.customize.Window;
import org.example.ttms.customize.Pop_ups;
import org.example.ttms.entityClass.train.Route;

import java.util.Arrays;
import java.util.List;

public class SearchRouteController {

    @FXML
    private StackPane stackPane;

    @FXML
    private TextField textField;

    @FXML
    private ChoiceBox<String> choiceBox;

    @FXML
    private Button searchButton;

    @FXML
    private TableView<Route> tableView;

    @FXML
    private TableColumn<Route, Integer> routeIdTableColumn;

    @FXML
    private TableColumn<Route, String> startStationColumn;

    @FXML
    private TableColumn<Route, String> endStationTableColumn;

    @FXML
    private TableColumn<Route, Integer> stationNumTableColumn;

    @FXML
    private TableColumn<Route, Void> detailTableColumn;

    @FXML
    private TableColumn<Route, Void> delTableColumn;

    @FXML
    private Label label;

    private String[] typeAll = {"路线编号", "站点名称"};

    private ObservableList<Route> observableList = FXCollections.observableArrayList();

    public StackPane getStackPane() {
        return stackPane;
    }

    public void setStackPane(StackPane stackPane) {
        this.stackPane = stackPane;
    }

    public TextField getTextField() {
        return textField;
    }

    public void setTextField(TextField textField) {
        this.textField = textField;
    }

    public ChoiceBox<String> getChoiceBox() {
        return choiceBox;
    }

    public void setChoiceBox(ChoiceBox<String> choiceBox) {
        this.choiceBox = choiceBox;
    }

    public Button getSearchButton() {
        return searchButton;
    }

    public void setSearchButton(Button searchButton) {
        this.searchButton = searchButton;
    }

    public TableView<Route> getTableView() {
        return tableView;
    }

    public void setTableView(TableView<Route> tableView) {
        this.tableView = tableView;
    }

    public TableColumn<Route, Integer> getRouteIdTableColumn() {
        return routeIdTableColumn;
    }

    public void setRouteIdTableColumn(TableColumn<Route, Integer> routeIdTableColumn) {
        this.routeIdTableColumn = routeIdTableColumn;
    }

    public TableColumn<Route, String> getStartStationColumn() {
        return startStationColumn;
    }

    public void setStartStationColumn(TableColumn<Route, String> startStationColumn) {
        this.startStationColumn = startStationColumn;
    }

    public TableColumn<Route, String> getEndStationTableColumn() {
        return endStationTableColumn;
    }

    public void setEndStationTableColumn(TableColumn<Route, String> endStationTableColumn) {
        this.endStationTableColumn = endStationTableColumn;
    }

    public TableColumn<Route, Integer> getStationNumTableColumn() {
        return stationNumTableColumn;
    }

    public void setStationNumTableColumn(TableColumn<Route, Integer> stationNumTableColumn) {
        this.stationNumTableColumn = stationNumTableColumn;
    }

    public TableColumn<Route, Void> getDetailTableColumn() {
        return detailTableColumn;
    }

    public void setDetailTableColumn(TableColumn<Route, Void> detailTableColumn) {
        this.detailTableColumn = detailTableColumn;
    }

    public TableColumn<Route, Void> getDelTableColumn() {
        return delTableColumn;
    }

    public void setDelTableColumn(TableColumn<Route, Void> delTableColumn) {
        this.delTableColumn = delTableColumn;
    }

    public Label getLabel() {
        return label;
    }

    public void setLabel(Label label) {
        this.label = label;
    }

    public String[] getTypeAll() {
        return typeAll;
    }

    public void setTypeAll(String[] typeAll) {
        this.typeAll = typeAll;
    }

    public ObservableList<Route> getObservableList() {
        return observableList;
    }

    public void setObservableList(ObservableList<Route> observableList) {
        this.observableList = observableList;
    }

    public void initialize(){
        choiceBox.getItems().addAll(typeAll);
        choiceBox.getSelectionModel().select(0);
        textField.setPromptText("输入路线编号");
        choiceBox.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observableValue,
                                String oldValue, String newValue) {
                if(newValue.equals("站点名称")){
                    textField.setPromptText("输入站点名称");
                }
                else{
                    textField.setPromptText("输入路线编号");
                }
            }
        });
        routeIdTableColumn.setCellValueFactory(cellData -> cellData.getValue().routeIdProperty().asObject());
        startStationColumn.setCellValueFactory(cellData -> cellData.getValue().startStationProperty());
        endStationTableColumn.setCellValueFactory(cellData -> cellData.getValue().endStationProperty());
        stationNumTableColumn.setCellValueFactory(cellData -> cellData.getValue().stationNumProperty().asObject());
        detailTableColumn.setCellFactory(new Callback<TableColumn<Route, Void>, TableCell<Route, Void>>() {
            @Override
            public TableCell<Route, Void> call(TableColumn<Route, Void> routeVoidTableColumn) {
                return new TableCell<Route, Void>(){
                    private final Button detailButton = new Button("查看详情");
                    {
                        detailButton.setOnAction(e -> {
                            Detail detail = new Detail();
                            detail.routeDetailWindow(getTableView().getItems().get(getIndex()));
                        });
                    }
                    @Override
                    public void updateItem(Void item, boolean empty){
                        super.updateItem(item, empty);
                        if(empty){
                            setGraphic(null);
                        }
                        else{
                            setGraphic(detailButton);
                        }
                    }
                };
            }
        });
        delTableColumn.setCellFactory(new Callback<TableColumn<Route, Void>, TableCell<Route, Void>>() {
            @Override
            public TableCell<Route, Void> call(TableColumn<Route, Void> routeVoidTableColumn) {
                return new TableCell<Route, Void>(){
                    private final Button delButton = new Button("  删除  ");
                    {
                        delButton.setOnAction(e -> {
                            Route route = getTableView().getItems().get(getIndex());
                            if(judgeDelRoute(route)){
                                observableList.remove(route);
                                route.delRoute(route.getRouteId());
                                route.decreaseRouteNum(route);
                            }
                        });
                    }
                    @Override
                    public void updateItem(Void item, boolean empty){
                        super.updateItem(item, empty);
                        if(empty){
                            setGraphic(null);
                        }
                        else{
                            setGraphic(delButton);
                        }
                    }
                };
            }
        });

        observableList.addListener((ListChangeListener.Change<? extends Route> c) -> {
            label.setVisible(observableList.isEmpty());
        });
        tableView.setItems(observableList);
    }

    @FXML
    void chickSearch(ActionEvent event) {
        Route route = new Route();
        Pop_ups popUps = new Pop_ups();
        if(choiceBox.getValue().equals("路线编号")){
            if(textField.getText().matches("^\\d+$")){
                Route route1 = route.obtainRoute(Integer.parseInt(textField.getText()));
                if(route1 == null){
                    popUps.pop_ups3("该路线不存在");
                }
                else{
                    observableList.clear();
                    observableList.add(route1);
                }
            }
            else{
                popUps.pop_ups3("该路线不存在");
            }
        }
        else{
            List<String> stationList = Arrays.asList(textField.getText().split(" +"));
            List<Route> routeList = route.obtainContainsRoute(stationList);
            observableList.clear();
            observableList.addAll(routeList);
        }
    }
    //判断是否可删除
    public boolean judgeDelRoute(Route route){
        Pop_ups popUps = new Pop_ups();
        if(route.getTripsNum() == 0){
            popUps.pop_ups4("路线删除成功");
            return true;
        }
        else{
            popUps.pop_ups3("该路线存在车次,无法删除");
            return false;
        }
    }
}
